<?php
/**
 * Created by PhpStorm.
 * User: HouJie
 * Date: 2018-4-3
 * Time: 15:14
 */

namespace Bqdove\Contracts;

use Psr\SimpleCache\CacheInterface as BaseCacheInterface;

abstract class CacheInterface implements BaseCacheInterface
{
    /**
     * 缓存服务实例
     * @var CacheInterface
     */
    protected $app;

    protected $config = [
        'preFix' => 'bqdove_cache',         // 前缀
        'expired' => 7200,      // 存储时间 分钟
        'is_zip' => 0,          // 是否压缩
        'zip_level' => 6,       // 压缩等级
    ];

    /**
     * 获取并删除
     * @param $key
     * @return mixed
     */
    abstract public function pull($key);

    /**
     *  永久存储
     * @param $key
     * @param $value
     * @return mixed
     */
    abstract public function forever($key, $value);

    /**
     * 自增缓存
     * @param $key
     * @param int $value
     * @return mixed
     */
    abstract public function increment($key, $value = 1);

    /**
     * 自减缓存
     * @param $key
     * @param int $value
     * @return mixed
     */
    abstract public function decrement($key, $value = 1);




    /**
     * 返回默认的值
     * @param $value
     * @return mixed
     */
    public function value($value)
    {
        return $value instanceof \Closure ? $value() : $value;
    }

    /**
     * 序列化
     * @param $value mixed 支持闭包
     * @return string
     */
    public function serialize($value): string
    {
        if (extension_loaded('igbinary')) {
            return igbinary_serialize($this->value($value));
        }
        return serialize($this->value($value));
    }

    /**
     * 解序列化
     * @param $value
     * @return mixed
     */
    public function unserialize($value)
    {
        if (extension_loaded("igbinary")) {
            return igbinary_unserialize($value);
        }

        return unserialize($value);
    }
}